package com.oracle.application.filter;

import com.alibaba.fastjson.JSONObject;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import com.oracle.application.config.JWTConfig;
import com.oracle.application.vo.R;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.MalformedJwtException;
import io.jsonwebtoken.SignatureException;
import io.jsonwebtoken.UnsupportedJwtException;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author bug制造者
 * @ClassName AuthFilter.java
 * @createTime 2021年07月06日 16:18:00
 * @Description
 */
@Data
@Component
@ConfigurationProperties(prefix = "auth")
public class AuthFilter extends ZuulFilter {

    private String[] filterUrl;

    @Autowired
    private JWTConfig jwtConfig;

    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 1;
    }

    @Override
    public boolean shouldFilter() {
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
        String requestURI = request.getRequestURI();
        for (String uri : filterUrl) {
            if (requestURI.contains(uri)){
                return true;
            }
        }
        return false;
    }

    @Override
    public Object run() throws ZuulException {
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
        HttpServletResponse response = currentContext.getResponse();
        String token = request.getHeader("token");
        if (token== null || "".equals(token)){
            response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
            R<Object> resultR = new R<>(401,"去登录",null);
            currentContext.setResponseBody(JSONObject.toJSONString(resultR));
            currentContext.setSendZuulResponse(false);
            return null;
        }
        try {
            jwtConfig.verifyToken(token);
        } catch (ExpiredJwtException e) {
            R<Object> resultR = new R<>(401,"令牌过期",null);
            currentContext.setResponseBody(JSONObject.toJSONString(resultR));
            currentContext.setSendZuulResponse(false);
        } catch (UnsupportedJwtException e) {
            R<Object> resultR = new R<>(401,"验证异常",null);
            currentContext.setResponseBody(JSONObject.toJSONString(resultR));
            currentContext.setSendZuulResponse(false);
        } catch (MalformedJwtException e) {
            R<Object> resultR = new R<>(401,"验证异常",null);
            currentContext.setResponseBody(JSONObject.toJSONString(resultR));
            currentContext.setSendZuulResponse(false);
        } catch (SignatureException e) {
            R<Object> resultR = new R<>(401,"验证异常",null);
            currentContext.setResponseBody(JSONObject.toJSONString(resultR));
            currentContext.setSendZuulResponse(false);
        } catch (IllegalArgumentException e) {
            R<Object> resultR = new R<>(401,"去登录",null);
            currentContext.setResponseBody(JSONObject.toJSONString(resultR));
            currentContext.setSendZuulResponse(false);
        }
        return null;
    }
}
